//! 日期时间生成器测试
//! 
//! 测试 src/generators/datetime.rs 中的日期时间生成功能

use dataforge::generators::datetime::*;
use chrono::{DateTime, NaiveDate, Utc};

#[test]
fn test_iso8601_generation() {
    for _ in 0..10 {
        let iso_date = iso8601();
        assert!(!iso_date.is_empty());
        
        // 验证ISO8601格式
        assert!(iso_date.contains('T'));
        assert!(iso_date.contains('Z') || iso_date.contains('+') || iso_date.contains('-'));
        
        // 尝试解析日期
        let parsed = DateTime::parse_from_rfc3339(&iso_date);
        assert!(parsed.is_ok(), "Failed to parse ISO8601 date: {}", iso_date);
        
        println!("ISO8601: {}", iso_date);
    }
}

#[test]
fn test_rfc2822_generation() {
    for _ in 0..10 {
        let rfc_date = rfc2822();
        assert!(!rfc_date.is_empty());
        
        // RFC2822格式应该包含星期几
        let weekdays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
        assert!(weekdays.iter().any(|&day| rfc_date.contains(day)));
        
        println!("RFC2822: {}", rfc_date);
    }
}

#[test]
fn test_timestamp_generation() {
    for _ in 0..10 {
        let ts = timestamp();
        assert!(ts > 0);
        
        // 时间戳应该在合理范围内（1970年到2100年）
        assert!(ts >= 0);
        assert!(ts < 4102444800); // 2100年1月1日的时间戳
        
        println!("时间戳: {}", ts);
    }
}

#[test]
fn test_birthday_generation() {
    for _ in 0..10 {
        let birthday = birthday();
        assert!(!birthday.is_empty());
        
        // 生日应该是过去的日期
        let parsed = NaiveDate::parse_from_str(&birthday, "%Y-%m-%d");
        assert!(parsed.is_ok(), "Failed to parse birthday: {}", birthday);
        
        let birth_date = parsed.unwrap();
        let today = chrono::Utc::now().date_naive();
        assert!(birth_date < today, "Birthday should be in the past: {}", birthday);
        
        // 生日应该在合理的年龄范围内（0-120岁）
        let age_days = today.signed_duration_since(birth_date).num_days();
        assert!(age_days >= 0);
        assert!(age_days <= 120 * 365); // 120岁
        
        println!("生日: {}", birthday);
    }
}

#[test]
fn test_date_between_generation() {
    let start_date = "2020-01-01";
    let end_date = "2023-12-31";
    
    for _ in 0..10 {
        let date = date_between(start_date, end_date);
        assert!(!date.is_empty());
        
        let parsed = NaiveDate::parse_from_str(&date, "%Y-%m-%d");
        assert!(parsed.is_ok(), "Failed to parse date: {}", date);
        
        let generated_date = parsed.unwrap();
        let start = NaiveDate::parse_from_str(start_date, "%Y-%m-%d").unwrap();
        let end = NaiveDate::parse_from_str(end_date, "%Y-%m-%d").unwrap();
        
        assert!(generated_date >= start && generated_date <= end,
                "Date {} should be between {} and {}", date, start_date, end_date);
        
        println!("范围内日期: {}", date);
    }
}

#[test]
fn test_time_generation() {
    for _ in 0..10 {
        let time = time();
        assert!(!time.is_empty());
        
        // 验证时间格式 HH:MM:SS
        let parts: Vec<&str> = time.split(':').collect();
        assert_eq!(parts.len(), 3);
        
        let hour: u32 = parts[0].parse().expect("Invalid hour");
        let minute: u32 = parts[1].parse().expect("Invalid minute");
        let second: u32 = parts[2].parse().expect("Invalid second");
        
        assert!(hour < 24);
        assert!(minute < 60);
        assert!(second < 60);
        
        println!("时间: {}", time);
    }
}

#[test]
fn test_datetime_generation() {
    for _ in 0..10 {
        let dt = datetime();
        assert!(!dt.is_empty());
        assert!(dt.contains(' ')); // 日期时间之间应该有空格
        
        let parts: Vec<&str> = dt.split(' ').collect();
        assert_eq!(parts.len(), 2);
        
        // 验证日期部分
        let date_parsed = NaiveDate::parse_from_str(parts[0], "%Y-%m-%d");
        assert!(date_parsed.is_ok(), "Invalid date part: {}", parts[0]);
        
        // 验证时间部分
        let time_parts: Vec<&str> = parts[1].split(':').collect();
        assert_eq!(time_parts.len(), 3);
        
        println!("日期时间: {}", dt);
    }
}

#[test]
fn test_weekday_generation() {
    for _ in 0..10 {
        let weekday = weekday();
        assert!(!weekday.is_empty());
        
        let valid_weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", 
                             "Friday", "Saturday", "Sunday"];
        assert!(valid_weekdays.contains(&weekday.as_str()));
        
        println!("星期: {}", weekday);
    }
}

#[test]
fn test_month_generation() {
    for _ in 0..10 {
        let month = month();
        assert!(!month.is_empty());
        
        let valid_months = ["January", "February", "March", "April", "May", "June",
                           "July", "August", "September", "October", "November", "December"];
        assert!(valid_months.contains(&month.as_str()));
        
        println!("月份: {}", month);
    }
}

#[test]
fn test_date_format_consistency() {
    for _ in 0..20 {
        let birthday = birthday();
        let iso_date = iso8601();
        let timestamp_val = timestamp();
        
        // 生日格式应该是 YYYY-MM-DD
        assert!(birthday.matches('-').count() == 2);
        assert_eq!(birthday.len(), 10);
        
        // ISO8601应该包含T和时区信息
        assert!(iso_date.contains('T'));
        
        // 时间戳应该是正整数
        assert!(timestamp_val > 0);
    }
}

#[test]
fn test_age_distribution() {
    let mut ages = Vec::new();
    
    for _ in 0..100 {
        let birthday = birthday();
        let birth_date = NaiveDate::parse_from_str(&birthday, "%Y-%m-%d").unwrap();
        let today = chrono::Utc::now().date_naive();
        let age = today.signed_duration_since(birth_date).num_days() / 365;
        ages.push(age);
    }
    
    // 年龄分布应该合理
    let min_age = *ages.iter().min().unwrap();
    let max_age = *ages.iter().max().unwrap();
    
    assert!(min_age >= 0);
    assert!(max_age <= 120);
    
    // 大部分年龄应该在0-100岁之间
    let reasonable_ages = ages.iter().filter(|&&age| age >= 0 && age <= 100).count();
    assert!(reasonable_ages >= 90); // 至少90%的年龄是合理的
}